package com.rjkj.medicalwaste.ui.home

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import com.rjkj.medicalwaste.adapter.WaitPackAdapter
import com.rjkj.medicalwaste.bean.PackGroupByDepartmentBean
import com.rjkj.medicalwaste.databinding.DialogOpenBoxBinding
import com.rjkj.medicalwaste.databinding.DialogPrintBinding
import com.rjkj.medicalwaste.databinding.DialogWaitPackDetailBinding
import com.rjkj.medicalwaste.databinding.FragmentDeviceDataTestBinding
import com.rjkj.medicalwaste.livedatabus.LiveDataBus
import com.rjkj.medicalwaste.utils.Encoder03Small
import com.rjkj.medicalwaste.viewmodels.BluetoothControlViewModel
import com.rjkj.medicalwaste.viewmodels.UserViewModel
import com.rjkj.medicalwaste.viewmodels.WaitPackDetailViewModel
import com.rjkj.medicalwaste.viewmodels.provider.MainViewModelProvider
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class WaitPackDetailDialog : DialogFragment() {
    private val waitPackDetailViewModel: WaitPackDetailViewModel by viewModels()
    private val userViewModel: UserViewModel by activityViewModels {
        MainViewModelProvider.provideUserModel(requireContext())
    }
    private val listAdapter = WaitPackAdapter(listOf())
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = DialogWaitPackDetailBinding.inflate(inflater, container, false)
        binding.lifecycleOwner = viewLifecycleOwner
        onSubscribeUi(binding)
        return binding.root
    }

    fun onSubscribeUi(binding: DialogWaitPackDetailBinding) {
        lifecycleScope.launchWhenResumed {
            waitPackDetailViewModel.getWaitPackDetail(
                url = userViewModel.getUrl(),
                deviceSn = userViewModel.getSerial(),
                hospitalId = userViewModel.hospitalId.value ?: ""
            )
        }
        binding.packList.apply {
            val dividerItemDecoration =
                DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
            addItemDecoration(dividerItemDecoration)
            layoutManager = LinearLayoutManager(requireContext())
            adapter = listAdapter
        }
        waitPackDetailViewModel.packList.observe(viewLifecycleOwner) {
            lifecycleScope.launch {
                Log.e("packList", "onSubscribeUi: ${it.toString()}")
                val list = it.groupBy { pack ->
                    pack.departmentQRCode
                }.map { map ->
                    PackGroupByDepartmentBean(
                        userViewModel.findUserByQrCode(map.key)?.name ?: "",
                        map.value.size
                    )
                }
                listAdapter.list = list
                listAdapter.notifyDataSetChanged()
            }


        }
    }

    override fun onStart() {
        dialog!!.window!!.apply {
            attributes.gravity = Gravity.CENTER//位于底部
            decorView.background = ColorDrawable(Color.TRANSPARENT)//背景色
        }
        super.onStart()
    }

}